package io.renren.modules.app.controller;

import java.util.*;

import com.aliyun.oss.event.ProgressPublisher;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.app.entity.BsBookEntity;
import io.renren.modules.app.entity.BsBookcategoryEntity;
import io.renren.modules.app.entity.CountBookNumVo;
import io.renren.modules.app.service.BsBookService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


/**
 * 图书表
 *
 * @author zwl
 * @email ${email}
 * @date 2024-01-01 16:55:09
 */
@RestController
@RequestMapping("bs_book/bsbook")
public class BsBookController {
    @Autowired
    private BsBookService bsBookService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("bs_book:bsbook:list")
    public R list(@RequestParam Map<String, Object> params, HttpServletRequest request){
        PageUtils page = bsBookService.queryPage(params,request);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("bs_book:bsbook:info")
    public R info(@PathVariable("id") Integer id,HttpServletRequest request){
        BsBookEntity bsBook = bsBookService.queryById(id,request);
//		BsBookEntity bsBook = bsBookService.getById(id);

        return R.ok().put("bsBook", bsBook);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("bs_book:bsbook:save")
    public R save(@RequestBody BsBookEntity bsBook){
        bsBook.setCreateTime(new Date());
        bsBook.setUpdateTime(new Date());
        bsBookService.saveBook(bsBook);
		//bsBookService.save(bsBook);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("bs_book:bsbook:update")
    public R update(@RequestBody BsBookEntity bsBook){
        bsBook.setUpdateTime(new Date());
        bsBookService.updateBookById(bsBook);
		//bsBookService.updateById(bsBook);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("bs_book:bsbook:delete")
    public R delete(@RequestBody Integer[] ids){
		bsBookService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 图书种类下拉框
     */
    @RequestMapping("/getBookcatename")
    public  R getBookcatename(){
        List<BsBookcategoryEntity> list = bsBookService.getBookcatename();
        return R.ok(list) ;
    }

    /**
     * 图片上传接口
     */
    @RequestMapping(value = "bookUpload",method = RequestMethod.POST)
    public R dishUpload(MultipartFile file) {
        String url = bsBookService.bookUpload(file);
        System.out.println(file);
        return R.ok(url);
    }

    /**
     * 查询借阅图书数量 按书名划分
     */
    @RequestMapping(value = "countBookNum",method = RequestMethod.GET)
    public R countBookNum(){
        List<CountBookNumVo> countBookNumVo = bsBookService.countBookNum();
        return R.ok(countBookNumVo);
    }
    /**
     * 查询借阅图书数量 按图书类别划分
     */
    @RequestMapping(value = "countBookCert",method = RequestMethod.GET)
    public R countBookCert(){
        List<CountBookNumVo> countBookNumVo = bsBookService.countBookCert();
        return R.ok(countBookNumVo);
    }
}
